package freespiritsocial.io.user.ui.fragment.post

import android.os.Bundle
import android.view.View
import androidx.lifecycle.lifecycleScope
import androidx.viewpager2.widget.ViewPager2
import com.shuyu.gsyvideoplayer.GSYVideoManager
import freespiritsocial.io.user.R
import freespiritsocial.io.user.base.BaseFragment
import freespiritsocial.io.user.data.model.bean.PostPojo
import freespiritsocial.io.user.databinding.FrgExplorePostDetailBinding
import freespiritsocial.io.user.ui.activity.MainAct
import freespiritsocial.io.user.ui.adapter.ExplorePostAdapter
import freespiritsocial.io.user.viewmodel.ExploreViewModel
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

/**
 * @description 帖子详情
 */
class ExplorePostDetailFrg: BaseFragment<ExploreViewModel,FrgExplorePostDetailBinding>() {

    override fun onViewCreated(view: View,savedInstanceState: Bundle?) {
        //设置view model作用域
        viewModelOwner = (activity as MainAct).exploreViewModelOwner
        super.onViewCreated(view,savedInstanceState)
    }

    private val postLists = arrayListOf<PostPojo>()
    private val position: Int by lazy {
        arguments?.getInt("position") ?: mDatabind.vpDetail.currentItem
    }

    private var isLoading = false

    private val vpCallback by lazy {
        object: ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                playPosition(position)
            }
        }
    }

    private fun playPosition(position: Int) {
        eventViewModel.playPosition.value = position
    }

    override fun layoutId(): Int = R.layout.frg_explore_post_detail
    val adapter by lazy(LazyThreadSafetyMode.NONE) {
        ExplorePostAdapter(this,postLists)
    }

    override fun dismissLoading() {
        super.dismissLoading()
        mDatabind.refresh.finishRefresh()
        mDatabind.refresh.finishLoadMore()
    }


    override fun initView(savedInstanceState: Bundle?) = with(mDatabind) {
        //列表缓存上下5条数据
        vpDetail.offscreenPageLimit = 1
        vpDetail.adapter = adapter
        vpDetail.registerOnPageChangeCallback(vpCallback)
        vpDetail.post {
            playPosition(position)
        }
        refresh.setOnRefreshListener {
            mViewModel.getPostList(true)
        }
        refresh.setOnLoadMoreListener {
            isLoading = true
            mViewModel.getPostList()
        }
        refresh.setFooterHeight(0f)
        refresh.setDisableContentWhenLoading(true)
        return@with
    }

    override fun createObserver() {
        super.createObserver()
        lifecycleScope.launch {
            mViewModel.postResult.collect {
                if (it.isRefresh) postLists.clear()
                postLists.addAll(it.data)
                adapter.notifyDataSetChanged()
                mDatabind.refresh.setEnableLoadMore(it.hasMore)
                if (isLoading) {
                    mDatabind.vpDetail.currentItem = ++mDatabind.vpDetail.currentItem
                    isLoading = false
                }
            }
        }
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (hidden) {
            GSYVideoManager.onPause()
        } else {
            GSYVideoManager.onResume(false)
        }
    }

    override fun onPause() {
        super.onPause()
        GSYVideoManager.onPause()
    }

    override fun onResume() {
        super.onResume()
        GSYVideoManager.onResume(false)
        mDatabind.vpDetail.setCurrentItem(position,false)
        arguments?.remove("position")
    }

    override fun onDestroyView() {
        super.onDestroyView()
        mDatabind.vpDetail.registerOnPageChangeCallback(vpCallback)
        GSYVideoManager.releaseAllVideos()
    }
}